/*
 * DumperWrapper.java	2008/11/16
 *
 * Copyright 2008 Web & Zen Multimedia, All rights reserved.
 * Use is subject to license terms.
 */
package it.wzm.lightweightlog.annotation;

import it.wzm.lightweightlog.Dumper;
import it.wzm.lightweightlog.DumperException;
import it.wzm.lightweightlog.Entry;
import it.wzm.lightweightlog.LightweightlogInitializationException;
import it.wzm.lightweightlog.Constant.Severity;


/**
 * Model Class for implementing decorator pattern on Dumper. Currently developed decoration are: 
 * <ul>
 * <li>OpeningProcessor: take care of opening and closing the channel for writing</li>
 * <li>Dumping processor </li>
 * </ul>
 * All of them can be customized by changing the property exposed via annotation. 
 * @author Leonardo Celati
 */
public abstract class DumperWrapper extends Dumper {

	/**
	 * As required by serialization
	 */
	private static final long serialVersionUID = 7822652635014149651L;
	/**
	 * Holder for Dumper
	 */
	private Dumper d;
	
	/**
	 * Construct a valid Dumper passing the existing Dumper, this is the decorator pattern.
	 * @param d valid Dumper
	 */	
	protected DumperWrapper(Dumper d) {
		//super(d);
		// cannot accept null value for Dumper
		if (d==null)
			throw new LightweightlogInitializationException("Dumper null");

		// otherwise...
		this.d=d;
	}

	/**
	 * Getter for Dumper
	 * @return the d
	 */
	protected final Dumper getDumper() {
		return this.d;
	}
	
	/**
	 * Getter for name
	 */
	@Override
	public final String getName() {
		return this.d.getName();
	}
	
    /**
     * Getter for kill on failure
     * @return Severity
     */
    public boolean hasKillOnFailure() { 
        return this.d.hasKillOnFailure();
    }
    
    /**
     * We want this dumper always open
     * @return Severity
     */
    public boolean isKeepAlive() {
        return this.d.isKeepAlive();
    }
	   
	/**
	 * Implementing overridden method
	 */
	@Override
	public final boolean isOpen() {
		return this.d.isOpen();
	}
	
	/**
	 * Getter for severity
	 */
	@Override
	public final Severity getSeverity() {
		return d.getSeverity();
	}
	
	/**
	 * Implementing overridden method
	 */
	@Override
	public synchronized void open() throws DumperException {
		this.d.open();
		this.notifyAll();
	}
	
    /**
     * Dump Entry, the method from 'Dumper:d' is used
     * @param e Anything extending Entry
     */
    @Override
    public synchronized <E extends Entry> void dump(E e) throws DumperException {    	
        this.d.dump(e);
        this.notifyAll();
    }
	
    /**
     * Close 
     */
	@Override
	public synchronized void close() throws DumperException {
		this.d.close();
		this.notifyAll();
	}
	
}
